var e, t, a, r, n, s, o, u, i, c = require("../../@babel/runtime/helpers/interopRequireDefault"), l = c(require("../../@babel/runtime/regenerator")), d = require("../../@babel/runtime/helpers/asyncToGenerator"), p = require("../../@babel/runtime/helpers/defineProperty"), g = c(require("../../utils/api")), f = c(require("../../utils/request")), h = getApp();

Page({
    data: (e = {
        statusBarHeight: h.globalData.statusBarHeight,
        resourceUrl: h.globalData.resourceUrl,
        name: "",
        isParty: !1,
        rankImg: null,
        headImg: "",
        companyName: "",
        signList: [],
        signDays: 0,
        month: "",
        todaySign: !1,
        companyCultureList: [],
        scoreStep: 0,
        score: 0,
        restMemberScore: 0,
        signDialog: !1,
        audioList: []
    }, p(e, "companyCultureList", []), p(e, "showDialog", !1), p(e, "innerAudioContext", null), 
    p(e, "play", !0), p(e, "signDialog", !1), p(e, "firstSign", !1), p(e, "todayScore", ""), 
    p(e, "cardSum", null), p(e, "cardType", null), p(e, "cardDetail", null), e),
    debounce: function(e, t) {
        var a = arguments.length > 2 && void 0 !== arguments[2] && arguments[2], r = null;
        return function() {
            var n = arguments, s = this;
            r && clearTimeout(r), a ? (r || e.apply(this, arguments), r = setTimeout(function() {
                r = null;
            }, t)) : r = setTimeout(function() {
                e.apply(s, n);
            }, t);
        };
    },
    getAudioList: (i = d(l.default.mark(function e() {
        var t, a;
        return l.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                return t = wx.getStorageSync("openid"), e.prev = 1, e.next = 4, g.default.audioList({
                    openid: t
                });

              case 4:
                a = e.sent, this.setData({
                    audioList: a.result
                }), this.data.audioList.length && this.playMusic(0), e.next = 12;
                break;

              case 9:
                e.prev = 9, e.t0 = e.catch(1), console.log(e.t0);

              case 12:
              case "end":
                return e.stop();
            }
        }, e, this, [ [ 1, 9 ] ]);
    })), function() {
        return i.apply(this, arguments);
    }),
    getSignList: (u = d(l.default.mark(function e() {
        var t, a, r, n, s, o;
        return l.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                return t = wx.getStorageSync("openid"), e.prev = 1, e.next = 4, g.default.signList({
                    openid: t
                });

              case 4:
                if (a = e.sent, !(r = a.result.list).length) {
                    e.next = 34;
                    break;
                }
                e.t0 = r[0].month, e.next = 1 === e.t0 ? 10 : 2 === e.t0 ? 12 : 3 === e.t0 ? 14 : 4 === e.t0 ? 16 : 5 === e.t0 ? 18 : 6 === e.t0 ? 20 : 7 === e.t0 ? 22 : 8 === e.t0 ? 24 : 9 === e.t0 ? 26 : 10 === e.t0 ? 28 : 11 === e.t0 ? 30 : 12 === e.t0 ? 32 : 34;
                break;

              case 10:
                return n = "一月", e.abrupt("break", 34);

              case 12:
                return n = "二月", e.abrupt("break", 34);

              case 14:
                return n = "三月", e.abrupt("break", 34);

              case 16:
                return n = "四月", e.abrupt("break", 34);

              case 18:
                return n = "五月", e.abrupt("break", 34);

              case 20:
                return n = "六月", e.abrupt("break", 34);

              case 22:
                return n = "七月", e.abrupt("break", 34);

              case 24:
                return n = "八月", e.abrupt("break", 34);

              case 26:
                return n = "九月", e.abrupt("break", 34);

              case 28:
                return n = "十月", e.abrupt("break", 34);

              case 30:
                return n = "十一月", e.abrupt("break", 34);

              case 32:
                return n = "十二月", e.abrupt("break", 34);

              case 34:
                this.setData({
                    signList: a.result.list,
                    month: n,
                    signDays: a.result.signDays
                }), s = new Date().getDate() - 1, o = "+" + a.result.list[s].scoreStep, this.setData({
                    todayScore: o
                }), e.next = 43;
                break;

              case 40:
                e.prev = 40, e.t1 = e.catch(1), console.log(e.t1);

              case 43:
              case "end":
                return e.stop();
            }
        }, e, this, [ [ 1, 40 ] ]);
    })), function() {
        return u.apply(this, arguments);
    }),
    getCompanyCulture: (o = d(l.default.mark(function e() {
        var t, a, r;
        return l.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                return t = wx.getStorageSync("openid"), e.prev = 1, e.next = 4, g.default.companyCulture({
                    openid: t
                });

              case 4:
                a = e.sent, r = a.result.map(function(e) {
                    return e.desc = e.desc.replace(/\\n/g, "\n"), e;
                }), this.setData({
                    companyCultureList: r
                }), e.next = 12;
                break;

              case 9:
                e.prev = 9, e.t0 = e.catch(1), console.log(e.t0.msg);

              case 12:
              case "end":
                return e.stop();
            }
        }, e, this, [ [ 1, 9 ] ]);
    })), function() {
        return o.apply(this, arguments);
    }),
    getUserInfo: function() {
        var e = this;
        return d(l.default.mark(function t() {
            var a, r, n;
            return l.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return a = wx.getStorageSync("openid"), t.prev = 1, t.next = 4, g.default.userInfo({
                        openid: a
                    });

                  case 4:
                    r = t.sent, h.globalData.userInfo.name = r.result.name, h.globalData.userInfo.headImg = r.result.headImg, 
                    h.globalData.userInfo.rankImg = r.result.gradeImage, h.globalData.userInfo.isParty = r.result.isParty, 
                    h.globalData.userInfo.companyName = r.result.companyName, h.globalData.userInfo.companyId = r.result.companyId, 
                    h.globalData.userInfo.partyBranchName = r.result.partyBranchName, n = r.result.memberScore, 
                    h.globalData.scoreboard = n, h.globalData.restMemberScore = r.result.restMemberScore, 
                    e.setData({
                        score: n,
                        restMemberScore: r.result.restMemberScore,
                        name: h.globalData.userInfo.name,
                        isParty: r.result.isParty,
                        rankImg: r.result.gradeImage,
                        headImg: h.globalData.userInfo.headImg,
                        companyName: h.globalData.userInfo.companyName,
                        todaySign: r.result.todaySign
                    }), e.data.todaySign || (e.getCompanyCulture(), e.data.companyCultureList.length && e.setData({
                        showDialog: !0
                    })), t.next = 22;
                    break;

                  case 19:
                    t.prev = 19, t.t0 = t.catch(1), console.log(t.t0.msg);

                  case 22:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 1, 19 ] ]);
        }))();
    },
    getUserScore: function() {
        var e = this;
        return d(l.default.mark(function t() {
            var a, r, n;
            return l.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return a = wx.getStorageSync("openid"), t.prev = 1, t.next = 4, g.default.userInfo({
                        openid: a
                    });

                  case 4:
                    r = t.sent, n = r.result.memberScore, h.globalData.scoreboard = n, h.globalData.restMemberScore = r.result.restMemberScore, 
                    e.setData({
                        score: n,
                        restMemberScore: r.result.restMemberScore
                    }), t.next = 14;
                    break;

                  case 11:
                    t.prev = 11, t.t0 = t.catch(1), console.log(t.t0.msg);

                  case 14:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 1, 11 ] ]);
        }))();
    },
    getCardInfo: function() {
        var e = this;
        return d(l.default.mark(function t() {
            var a, r, n;
            return l.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return a = wx.getStorageSync("openid"), t.prev = 1, t.next = 4, g.default.getCardDetail({
                        openid: a
                    });

                  case 4:
                    r = t.sent, n = r.result, e.setData({
                        cardSum: n
                    }), t.next = 12;
                    break;

                  case 9:
                    t.prev = 9, t.t0 = t.catch(1), console.log(t.t0.msg);

                  case 12:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 1, 9 ] ]);
        }))();
    },
    onLoad: function() {
        var e, t = this;
        wx.getStorageSync("openid") || wx.login({
            success: (e = d(l.default.mark(function e(a) {
                var r;
                return l.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return e.prev = 0, e.next = 3, g.default.openid({
                            code: a.code
                        });

                      case 3:
                        0 === (r = e.sent).result.ifReg ? wx.redirectTo({
                            url: "/pages/login/login"
                        }) : (wx.setStorageSync("openid", r.result.openid), t.getUserInfo(), t.getSignList(), 
                        t.getCardInfo()), e.next = 10;
                        break;

                      case 7:
                        e.prev = 7, e.t0 = e.catch(0), console.log(3, e.t0, e.t0.msg);

                      case 10:
                      case "end":
                        return e.stop();
                    }
                }, e, null, [ [ 0, 7 ] ]);
            })), function(t) {
                return e.apply(this, arguments);
            })
        });
    },
    onShow: (s = d(l.default.mark(function e() {
        return l.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                if (wx.getStorageSync("openid")) {
                    e.next = 3;
                    break;
                }
                return e.abrupt("return");

              case 3:
                this.getUserInfo(), this.getSignList(), this.getCardInfo();

              case 6:
              case "end":
                return e.stop();
            }
        }, e, this);
    })), function() {
        return s.apply(this, arguments);
    }),
    onShareAppMessage: function() {
        return {
            title: this.data.name + "已经签到" + this.data.signDays + "天了 快来参加吧！",
            path: "/pages/index/index"
        };
    },
    onUnload: (n = d(l.default.mark(function e() {
        var t;
        return l.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                (t = this.data.innerAudioContext) && t.destroy();

              case 2:
              case "end":
                return e.stop();
            }
        }, e, this);
    })), function() {
        return n.apply(this, arguments);
    }),
    isMonday: function() {
        return 1 == new Date().getDay();
    },
    touchMove: function() {},
    showSignCard: function() {
        this.setData({
            signDialog: !0
        }), wx.hideTabBar();
    },
    closeCard: function() {
        this.setData({
            signDialog: !1,
            firstSign: !1
        }), wx.showTabBar();
    },
    signInDebounce: function() {
        var e = this;
        this.isMonday() ? wx.getSetting({
            success: function(t) {
                var a;
                t.authSetting["scope.userInfo"] && wx.getUserProfile({
                    desc: "用于更新用户资料",
                    success: (a = d(l.default.mark(function t(a) {
                        var r;
                        return l.default.wrap(function(t) {
                            for (;;) switch (t.prev = t.next) {
                              case 0:
                                if (h.globalData.authorized = !0, "" !== (r = wx.getStorageSync("openid"))) {
                                    t.next = 4;
                                    break;
                                }
                                return t.abrupt("return");

                              case 4:
                                return t.prev = 4, t.next = 7, (0, f.default)({
                                    method: "post",
                                    url: h.globalData.baseUrl + "/wxapp/updateMember",
                                    data: {
                                        openid: r,
                                        headImg: a.userInfo.avatarUrl
                                    }
                                });

                              case 7:
                                h.globalData.userInfo.headImg = a.userInfo.avatarUrl, e.debounce(e.signIn, 500, !1)(), 
                                t.next = 14;
                                break;

                              case 11:
                                t.prev = 11, t.t0 = t.catch(4), console.log(t.t0);

                              case 14:
                              case "end":
                                return t.stop();
                            }
                        }, t, null, [ [ 4, 11 ] ]);
                    })), function(e) {
                        return a.apply(this, arguments);
                    }),
                    fail: function(e) {
                        console.log(e);
                    }
                });
            }
        }) : this.debounce(this.signIn, 500, !1)();
    },
    signIn: (r = d(l.default.mark(function e() {
        var t, a, r, n, s, o;
        return l.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                if (h.globalData.authorized && "" !== h.globalData.userInfo.headImg) {
                    e.next = 3;
                    break;
                }
                return wx.navigateTo({
                    url: "/pages/authorize/authorize"
                }), e.abrupt("return");

              case 3:
                return t = wx.getStorageSync("openid"), e.prev = 4, e.next = 7, g.default.signIn({
                    openid: t
                });

              case 7:
                r = e.sent, n = "", r.result.rewards.forEach(function(e, t) {
                    n += 0 === t ? e.name + " +" + e.rewardAdd : "\r\n" + e.name + " +" + e.rewardAdd;
                }), "" !== n && wx.showToast({
                    title: n,
                    icon: "none",
                    duration: 2e3
                }), s = "signList[" + (r.result.signDate.day - 1) + "].selected", o = "signList[" + (r.result.signDate.day - 1) + "].scoreStep", 
                this.setData((a = {
                    todayScore: r.result.signDate.scoreStep,
                    score: r.result.memberScore
                }, p(a, s, r.result.signDate.selected), p(a, o, r.result.signDate.scoreStep), p(a, "signDays", r.result.signDays), 
                p(a, "firstSign", !0), p(a, "signDialog", !0), a)), this.getUserInfo(), this.getCardInfo(), 
                e.next = 21;
                break;

              case 18:
                e.prev = 18, e.t0 = e.catch(4), console.log(e.t0.msg);

              case 21:
              case "end":
                return e.stop();
            }
        }, e, this, [ [ 4, 18 ] ]);
    })), function() {
        return r.apply(this, arguments);
    }),
    toMyCoin: function() {
        wx.navigateTo({
            url: "/home/pages/scoreRecord/scoreRecord"
        });
    },
    toMyCard: function() {
        wx.navigateTo({
            url: "/home/pages/cardRecord/cardRecord"
        });
    },
    toRule: function() {
        wx.navigateTo({
            url: "/home/pages/rule/rule"
        });
    },
    toAnswer: function() {
        wx.switchTab({
            url: "/pages/answer/answer"
        });
    },
    swiperChange: function(e) {
        this.data.innerAudioContext.pause(), this.playMusic(e.detail.current);
    },
    bindplay: (a = d(l.default.mark(function e(t) {
        var a, r;
        return l.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                if (a = this.data.innerAudioContext, this.data.play) a.pause(), this.setData({
                    play: !1
                }); else {
                    a.play();
                    try {
                        r = wx.getStorageSync("openid"), g.default.addClickCount({
                            openid: r,
                            displayId: t.currentTarget.dataset.id
                        });
                    } catch (e) {
                        console.log(e);
                    }
                    this.setData({
                        play: !0
                    });
                }

              case 2:
              case "end":
                return e.stop();
            }
        }, e, this);
    })), function(e) {
        return a.apply(this, arguments);
    }),
    playMusic: function(e) {
        var t = wx.createInnerAudioContext();
        t.src = this.data.resourceUrl + this.data.audioList[e].url, t.onPlay(function() {
            console.log("开始播放");
        }), t.onError(function(e) {
            console.log(e.errMsg, e.errCode);
        }), this.setData({
            innerAudioContext: t,
            play: !1
        });
    },
    getCard: (t = d(l.default.mark(function e(t) {
        var a, r, n;
        return l.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                if (!t.currentTarget.dataset.abled) {
                    e.next = 3;
                    break;
                }
                return e.abrupt("return");

              case 3:
                return a = t.currentTarget.dataset.type, r = wx.getStorageSync("openid"), e.prev = 5, 
                e.next = 8, g.default.doCard({
                    openid: r,
                    cardType: a
                });

              case 8:
                3 == (n = e.sent).result.type ? this.setData({
                    cardType: 3
                }) : 2 == n.result.type ? this.setData({
                    cardType: 2,
                    cardDetail: n.result.card
                }) : 1 == n.result.type && this.setData({
                    cardType: 1,
                    cardDetail: n.result.card
                }), this.getCardInfo(), this.getUserScore(), e.next = 17;
                break;

              case 14:
                e.prev = 14, e.t0 = e.catch(5), console.log(e.t0.msg);

              case 17:
              case "end":
                return e.stop();
            }
        }, e, this, [ [ 5, 14 ] ]);
    })), function(e) {
        return t.apply(this, arguments);
    }),
    closeDetailCard: function() {
        this.setData({
            cardType: null
        });
    }
});